import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'color.dart';

/*
*
*
* 多选弹框---Item
*
* */

class QualityDialogItem {
  QualityDialogItem({this.dataList,this.choiceList});

  //数据源
  List dataList = [];

  //查找的数据
  List searchList = [];

  //选中的数据
  List<ChoiceModel> choiceList = [];

  //多选是否选中
  bool mm = false;

  ///多选弹框测试
  Future<List<ChoiceModel>> dialogTestss(context) {
    print("===================$choiceList======================");
    return showDialog(
        context: context,
        builder: (context) {
          return StatefulBuilder(builder: (context, state) {
            return Dialog(
              child: Container(
                width: (1.sw / 4) * 3,
                height: (1.sh / 4) * 3,
                padding: EdgeInsets.all(16.h),
                decoration: BoxDecoration(
                  color: Colors.white,
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Container(
                      child: Text(
                        "请选择数据",
                        style: TextStyle(
                            fontSize: 16.sp, color: AppColor.font_black),
                      ),
                    ),
                    Container(
                      height: 40.h,
                      padding: EdgeInsets.only(
                          left: 16.w, right: 16.w, top: 10.h, bottom: 10.h),
                      margin: EdgeInsets.symmetric(vertical: 16.h),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(20.w),
                        border:
                            Border.all(width: 0.5.w, color: AppColor.font_grey),
                      ),
                      child: Row(
                        children: <Widget>[
                          Expanded(
                            child: TextField(
                              cursorColor: Colors.grey,
                              style: TextStyle(
                                  fontSize: 14.sp, color: AppColor.font_black),
                              decoration: InputDecoration(
                                hintText: '请输入搜索内容',
                                hintStyle: TextStyle(
                                    color: Colors.grey, fontSize: 14.sp),
                                hintMaxLines: 1,
                                isDense: true,
                                //取消TextFiled的焦点样式
                                contentPadding: EdgeInsets.symmetric(
                                    horizontal: 0, vertical: 0),
                                //内容内边距，影响高度
                                border: InputBorder.none,
                                counterText: '',
                              ),
                              onChanged: (value) {
                                searchList = [];
                                for (var i = 0; i < dataList.length; i++) {
                                  if (dataList[i].contains(value)) {
                                    searchList.add(searchList[i]);
                                  }
                                }
                                state(() {});
                              },
                            ),
                          ),
                          SizedBox(
                            width: 10.w,
                          ),
                          Image.asset(
                            "assets/icon/cancel.png",
                            height: 14.h,
                            width: 14.w,
                          ),
                        ],
                      ),
                    ),
                    Expanded(
                      child: ListView.builder(
                        itemBuilder: (BuildContext context, int index) {
                          ///修改问题完毕，修改mm的名字和属性优化
                          mm=false;
                          if(choiceList.isNotEmpty){
                            for(var i=0;i<choiceList.length;i++){
                              if(choiceList[i].key_value==dataList[index]){
                                mm=true;
                                break;
                              }
                            }
                          }
                          return dialogCheckTest(index,mm);
                        },
                        itemCount: searchList.isNotEmpty
                            ? searchList.length
                            : dataList.length,
                      ),
                    ),
                    Container(
                      height: 60.h,
                      child: Row(
                        children: <Widget>[
                          Text(
                            "取消",
                            style: TextStyle(
                                fontSize: 14.sp, color: AppColor.font_black),
                          ),
                          Spacer(),
                          GestureDetector(
                            onTap: (){
                              Navigator.of(context).pop(choiceList);
                            },
                            child: Text(
                              "确定",
                              style: TextStyle(
                                  fontSize: 14.sp, color: AppColor.font_black),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            );
          });
        });
  }



  Widget dialogCheckTest(int index,bool mm) {
    // String condition_val =searchList.isNotEmpty?searchList[index]: dataList[index];
    String condition_val = dataList[index];
    return StatefulBuilder(builder: (context, state) {
      return Container(
        decoration: BoxDecoration(
          border: Border(
            top: BorderSide(
              width: 0.5.h, //宽度
              color: Color.fromRGBO(233, 233, 233, 1), //边框颜色
            ),
          ),
        ),
        child: Row(
          children: <Widget>[
            Expanded(
              child: Text(
                '${index + 1}.$condition_val',
                style: TextStyle(fontSize: 14.sp, color: AppColor.font_black),
              ),
            ),
            Checkbox(
              // value: choiceList.isNotEmpty?(choiceList[index].key_id!=index?false:true):false,
              value: mm,
              onChanged: (value) {
                mm = value;
                //判断数据是否被选中
                if (mm) {
                  if (choiceList.isNotEmpty) {
                    for (var i = 0; i < choiceList.length; i++) {
                      if (choiceList[i].key_id != index) {
                        ChoiceModel choiceModel = ChoiceModel();
                        choiceModel.key_id = index;
                        choiceModel.key_value = dataList[index];
                        choiceList.add(choiceModel);
                        break;
                      }
                    }
                  }else{
                    ChoiceModel choiceModel = ChoiceModel();
                    choiceModel.key_id = index;
                    choiceModel.key_value = dataList[index];
                    choiceList.add(choiceModel);
                  }
                } else {
                  if (choiceList.isNotEmpty) {
                    for (var i = 0; i < choiceList.length; i++) {
                      if (choiceList[i].key_id == index ) {
                        choiceList.removeAt(i);
                        break;
                      }
                    }
                  }
                }

                print("=============选中后的数据：${choiceList.toString()}===========");
                state(() {
                });
              },
            ),
          ],
        ),
      );
    });
  }
}

class ChoiceModel {
  int key_id; //获取的选中的key_id
  String key_value;

  ChoiceModel({this.key_id, this.key_value});

  @override
  String toString() {
    return '选中的数据：{key_id: $key_id, key_value: $key_value}';
  } //选中的内容



}
